#include "memory.h"

STATIC DWORD dwMemory;
STATIC LPVOID lpvPages[0x8000];
STATIC DWORD dwStart, dwEnd;

VOID MmInit(VOID){
    dwMemory = TOTAL_MEMORY_BYTES;
    dwStart = 0;
    dwEnd = 0;
    //16MB reserved
    for(REG DWORD i = 0x1000000; i < dwMemory; i+=0x1000)
        lpvPages[dwEnd++] = (LPVOID)(DWORD_PTR)i;
}

LPVOID MmAllocPage(VOID){
    REG LPVOID lpvRes;
    if(dwStart == dwEnd)
        return NULL;
    lpvRes = lpvPages[dwStart++];
    dwStart &= 0x7FFF;
    return lpvRes;
}

STATUS MmFreePage(LPVOID lpvPage){
    if((DWORD_PTR)lpvPage & 0xFFF)
        return STATUS_INVAL;
    lpvPages[dwEnd++] = lpvPage;
    dwEnd &= 0x7FFF;
    return STATUS_NOERR;
}
